package com.sunny.l1;

import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

public class Solution {

    public int run(TreeNode root) {

        if(null == root)
            return 0;

        int res = Integer.MAX_VALUE;
        Queue<TreeInfo> queue = new LinkedBlockingQueue<>();
        queue.add(new TreeInfo(root, 1));
        while (!queue.isEmpty()){
            TreeInfo info = (TreeInfo) queue.poll();
            if(null == info.getNode().left && null == info.getNode().right){
                if(info.getLayer() < res){
                    res = info.getLayer();
                }
            }
            if(null != info.getNode().left){
                queue.add(new TreeInfo(info.getNode().left, info.getLayer()+1));
            }
            if(null != info.getNode().right){
                queue.add(new TreeInfo(info.getNode().right, info.getLayer()+1));
            }

        }

        return res;

    }

}

class TreeInfo{
    TreeNode node;
    int layer;

    public TreeInfo(TreeNode node, int layer) {
        this.node = node;
        this.layer = layer;
    }

    public TreeNode getNode() {
        return node;
    }

    public void setNode(TreeNode node) {
        this.node = node;
    }

    public int getLayer() {
        return layer;
    }

    public void setLayer(int layer) {
        this.layer = layer;
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
 }